package com.cty._01_Base._02_Singleton;

import java.io.ObjectStreamException;

/**
 * @Auther: cty
 * @Date: 2020/6/25 8:48
 * @Description: 饿汉式实现单例模式
 * @version: 1.0
 */
public class Singleton01 {
    private static Singleton01 instance = new Singleton01();

    private Singleton01(){
        if(instance != null)
            throw new RuntimeException("使用反射暴力访问私有构造器");
    }

    public static Singleton01 getInstance(){
        return instance;
    }

    // 反序列化时返回指定对象
    private Object readResolve() throws ObjectStreamException {
        return instance;
    }

}  // end Singleton01{}
